package com.mdd.front.controller.order;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.order.IOrderService;
import com.mdd.front.service.system.ISystemDevPayWayService;
import com.mdd.front.validate.OrderParam;
import com.mdd.front.validate.PageParam;
import com.mdd.front.vo.order.OrderDetailVo;
import com.mdd.front.vo.order.OrderListVo;
import com.mdd.front.vo.system.SystemDevPayWayDetailVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 订单管理
 * @author panweiliang
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Resource
    IOrderService iOrderService;
    @Resource
    ISystemDevPayWayService systemDevPayWayService;

    /**
     * 订单列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    public Object list(@Validated PageParam pageParam,
                       @RequestParam Map<String, String> params) {
        PageResult<OrderListVo> list = iOrderService.list(pageParam, params);
        return AjaxResult.success(list);
    }

    /**
     * 订单支付方式列表
     *
     * @author panweiliang
     * @param sence 场景值
     * @return Object
     */
    @GetMapping("/pay/way/list")
    public Object listPayWay(@RequestParam("scene") Integer scene, @RequestParam(value = "hidePayWay", required = false) Integer hidePayWay) {
        List<SystemDevPayWayDetailVo> list = systemDevPayWayService.list(scene, hidePayWay);
        return AjaxResult.success(list);
    }


    /**
     * 订单详情
     *
     * @author panweiliang
     * @param id 主键ID
     * @return Object
     */
    @GetMapping("/detail")
    public Object detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        OrderDetailVo detail = iOrderService.detail(id, userId);
        return AjaxResult.success(detail);
    }

    /**
     * 订单新增
     *
     * @author panweiliang
     * @param orderParam 参数
     * @return Object
     */
    @PostMapping("/add")
    public Object add(@Validated(value = OrderParam.create.class) @RequestBody OrderParam orderParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        orderParam.setUserId(userId);
        return AjaxResult.success(iOrderService.add(orderParam));
    }

    /**
     * 订单完成支付
     *
     * @author panweiliang
     * @param orderParam 参数
     * @return Object
     */
    @PostMapping("/paystatus/completed")
    public Object updateOrderPayStatusToPaid(@Validated(value = OrderParam.change.class) @RequestBody OrderParam orderParam) throws WxPayException {
        return AjaxResult.success(this.iOrderService.updateOrderPayStatusToPaid(orderParam.getId()));
    }

    /**
     * 订单删除
     *
     * @author panweiliang
     * @param orderParam 参数
     * @return Object
     */
    @PostMapping("/del")
    public Object del(@Validated(value = OrderParam.delete.class) @RequestBody OrderParam orderParam) {
        iOrderService.del(orderParam.getId());
        return AjaxResult.success();
    }

    /**
     * 订单取消
     *
     * @author panweiliang
     * @param orderParam 参数
     * @return Object
     */
    @PostMapping("/cancel")
    public Object cancel(@Validated(value = OrderParam.delete.class) @RequestBody OrderParam orderParam) {
        iOrderService.cancel(orderParam.getId());
        return AjaxResult.success();
    }



}
